package models

import (
	"time"
	"weiboxs.com/new_h_glasses/utils"
)

import (
	"github.com/beego/beego/v2/client/orm"
)

type EquipmentData struct {
	Id                  int64     `form:"data_id"`
	Archives            *Archives `orm:"rel(one)"`
	RightVision         string    `json:"right_vision"`          //祼眼
	LeftVision          string    `json:"left_vision"`           //祼眼
	RightCorrectVision  string    `json:"right_correct_vision"`  //矫正
	LeftCorrectVision   string    `json:"left_correct_vision"`   //矫正
	LeftBallDiameter    string    `json:"left_ball_diameter"`    //球镜
	RightBallDiameter   string    `json:"right_ball_diameter"`   //球镜
	LeftColumnDiameter  string    `json:"left_column_diameter"`  //柱镜
	RightColumnDiameter string    `json:"right_column_diameter"` //柱镜
	LeftAxial           string    `json:"left_axial"`            //轴向
	RightAxial          string    `json:"right_axial"`           //轴向
	EyeRightAxial       string    `json:"eye_right_axial"`       //右眼轴
	EyeLeftAxial        string    `json:"eye_left_axial"`        //左眼轴
	RightPressure       string    `json:"right_pressure"`        //眼压
	LeftPressure        string    `json:"left_pressure"`         //眼压
	Summary             string    `json:"summary"`
	CreatedAt           int64     `json:"created_at"`
	UpdatedAt           int64     `json:"updated_at"`
	IsNew               int       `json:"is_new"`
	EquipmentYg         string    `json:"equipment_yg"`
	EquipmentYy         string    `json:"equipment_yy"`
}

type EquipmentDataQueryParam struct {
	BaseQueryParam
	Filter   string
	Mobile   string
	Name     string `json:"name"`
	SchoolId int64  `json:"school_id"`
	Grade    string `json:"grade"`
	Class    string `json:"class"`
	Planid   string `json:"planid"`
}

func (a *EquipmentData) TableName() string {
	return EquipmentDataTableName()
}

func EquipmentDataTableName() string {
	return TableName("sc_equipment_data")
}

// 添加筛查信息
func (a *EquipmentData) Insert() (int64, error) {

	a.CreatedAt = time.Now().Unix()
	a.UpdatedAt = time.Now().Unix()

	return orm.NewOrm().Insert(a)
}

// 修改信息
func (a *EquipmentData) Update(fields ...string) error {

	a.UpdatedAt = time.Now().Unix()

	field, _ := utils.ReflectModelFields(a, fields...)

	if _, err := orm.NewOrm().Update(a, field...); err != nil {
		return err
	}
	return nil
}

// 单条档案查询
func GetEquipmentDataById(id int64) (*EquipmentData, error) {
	r := new(EquipmentData)
	r.Id = id
	err := orm.NewOrm().Read(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}

// 删除档案数据
func EquipmentDataDelete(id int64) error {
	to, _ := orm.NewOrm().Begin()
	if _, err := to.QueryTable(EquipmentDataTableName()).Filter("id", id).Delete(); err != nil { //删除子表数据
		to.Rollback()
		return err
	}
	to.Commit()
	return nil
}

// 查询分页数据
func EquipmentDataPageList(params *EquipmentDataQueryParam) ([]*EquipmentData, int64) {
	query := orm.NewOrm().QueryTable(EquipmentDataTableName())
	data := make([]*EquipmentData, 0)

	sortorder := "-Id"
	if len(params.Sort) > 0 {
		sortorder = params.Sort
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}
	cond := orm.NewCondition()

	if len(params.Mobile) > 0 {
		cond = cond.And("mobile", params.Mobile)
	}
	query = query.SetCond(cond)

	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&data)

	return data, total
}

// 查询档案信息
func GetEquipmentDataByArchives(id int64) ([]*EquipmentData, error) {
	equipment := make([]*EquipmentData, 0)
	_, err := orm.NewOrm().QueryTable(EquipmentDataTableName()).RelatedSel("Archives").Filter("archives_id", id).OrderBy("-id").Limit(10).All(&equipment)
	if err != nil {
		return nil, err
	}
	return equipment, nil
}

// 按档案ID获取数据
func GetEquipmentDataByScreeningId(id, starttime, endtime int64) (*EquipmentData, error) {
	r := &EquipmentData{}
	o := orm.NewOrm().QueryTable(EquipmentDataTableName()).RelatedSel("Archives").Filter("archives_id", id)
	if starttime > 0 && endtime > 0 {
		o = o.Filter("created_at__gt", starttime).Filter("created_at__lt", endtime)
	}
	o.One(r)
	return r, nil
}
